package com.bk.game.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.input.GestureDetector.GestureListener;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.bk.game.global.BkGlobal;

/**
 * 描述:帮助
 * 
 * @author linqiang(866116)
 * @Since 2012-9-5
 */
public class HelpScreen implements Screen, GestureListener, InputProcessor {

	private TextureAtlas pager;
	private SpriteBatch batch;

	private Sprite closeBtn;
	private Sprite nextBtn;
	private Rectangle closeRect, nextRect;

	private TextureRegion[] pages;
	// 用来存储长和宽的值
	private int min;
	private int max;
	// 用来标记第一张图片的位置，可以为负，无法显示的部分就不会被画出
	private int position;
	private int index;
	private boolean scolling;

	@Override
	public void dispose() {
		pager.dispose();
		batch.dispose();
	}

	@Override
	public void hide() {

	}

	@Override
	public void pause() {

	}

	@Override
	public void render(float arg0) {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
		batch.begin();
		for (int i = 0; i < pages.length; i++)
			batch.draw(pages[i], position + i * min, 0, min, max);
		closeBtn.draw(batch);
		nextBtn.draw(batch);
		batch.end();
		if (scolling) {
			position -= 20 * min / 480f;
			System.out.println();
			if (position <= -index * min) {
				position = -index * min;
				scolling = false;
			}
		}

	}

	@Override
	public void resize(int arg0, int arg1) {

	}

	@Override
	public void resume() {

	}

	@Override
	public void show() {
		Gdx.input.setCatchBackKey(true);
		max = Gdx.graphics.getHeight() > Gdx.graphics.getWidth() ? Gdx.graphics.getHeight() : Gdx.graphics.getWidth();
		min = Gdx.graphics.getHeight() < Gdx.graphics.getWidth() ? Gdx.graphics.getHeight() : Gdx.graphics.getWidth();

		batch = new SpriteBatch(); // 实例化
		pager = BkGlobal.manager.get("help.pack", TextureAtlas.class);
		pages = new TextureRegion[7];
		pages[0] = pager.findRegion("help1");
		pages[1] = pager.findRegion("help2");
		pages[2] = pager.findRegion("help3");
		pages[3] = pager.findRegion("help4");
		pages[4] = pager.findRegion("help5");
		pages[5] = pager.findRegion("help6");
		pages[6] = pager.findRegion("help7");

		closeBtn = new Sprite(pager.findRegion("btn_back"));
		float w = 106;
		closeBtn.setSize(w * BkGlobal.factorx, w * BkGlobal.factory);
		closeBtn.setPosition(22 * BkGlobal.factorx, 770 * BkGlobal.factory);
		closeRect = closeBtn.getBoundingRectangle();
		closeBtn.setColor(0.5f, 0.5f, 0.5f, 0f);

		nextBtn = new Sprite(pager.findRegion("btn_next"));
		nextBtn.setSize(w * BkGlobal.factorx, w * BkGlobal.factory);
		nextBtn.setPosition(518 * BkGlobal.factorx, 26 * BkGlobal.factory);
		nextRect = nextBtn.getBoundingRectangle();
		nextBtn.setColor(0.5f, 0.5f, 0.5f, 0f);

		// 将当前Screen加入按键和手势监听队列
		InputMultiplexer multiplexer = new InputMultiplexer();
		multiplexer.addProcessor(this);
		multiplexer.addProcessor(new GestureDetector(this));
		Gdx.input.setInputProcessor(multiplexer);
	}

	@Override
	public boolean fling(float arg0, float arg1) {
		closeBtn.setColor(0.5f, 0.5f, 0.5f, 0f);
		nextBtn.setColor(0.5f, 0.5f, 0.5f, 0f);
		return false;
	}

	@Override
	public boolean longPress(int arg0, int arg1) {
		return false;
	}

	@Override
	public boolean pan(int arg0, int arg1, int arg2, int arg3) {
		return false;
	}

	@Override
	public boolean pinch(Vector2 arg0, Vector2 arg1, Vector2 arg2, Vector2 arg3) {
		return false;
	}

	@Override
	public boolean tap(int x, int y, int count) {
		int ty = Gdx.graphics.getHeight() - y;
		if (closeRect.contains(x, ty)) {
			closeBtn.setColor(0.5f, 0.5f, 0.5f, 0f);
			BkGlobal.currentGame.setScreen(BkGlobal.mainScreen);
		} else if (nextRect.contains(x, ty)) {
			nextBtn.setColor(0.5f, 0.5f, 0.5f, 0f);
			index++;
			if (index >= 7) {
				BkGlobal.currentGame.setScreen(BkGlobal.mainScreen);
			} else {
				scolling = true;
			}
		}
		return false;
	}

	@Override
	public boolean touchDown(int x, int y, int pointer) {
		int ty = Gdx.graphics.getHeight() - y;
		if (closeRect.contains(x, ty)) {
			BkGlobal.btnSound.play();
			closeBtn.setColor(0.5f, 0.5f, 0.5f, 1f);
		} else if (nextRect.contains(x, ty)) {
			BkGlobal.btnSound.play();
			nextBtn.setColor(0.5f, 0.5f, 0.5f, 1f);
		}
		return false;
	}

	@Override
	public boolean zoom(float arg0, float arg1) {
		return false;
	}

	@Override
	public boolean keyDown(int arg0) {
		return false;
	}

	@Override
	public boolean keyTyped(char arg0) {
		return false;
	}

	@Override
	public boolean keyUp(int arg0) {
		if (arg0 == Input.Keys.BACK) {
			BkGlobal.currentGame.setScreen(BkGlobal.mainScreen);
		}
		return false;
	}

	@Override
	public boolean scrolled(int arg0) {
		return false;
	}

	@Override
	public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {
		return false;
	}

	@Override
	public boolean touchDragged(int arg0, int arg1, int arg2) {
		return false;
	}

	@Override
	public boolean touchMoved(int arg0, int arg1) {
		return false;
	}

	@Override
	public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
		return false;
	}
}
